//
//  BaseViewController.swift
//  Base
//
//  Created by 元穗 on 2017/5/23.
//  Copyright © 2017年 敬信. All rights reserved.
//

import Extend
import SwiftyJSON
import HandyJSON

open class BaseViewCon: ViewConEx {

    //注册网络状态通知
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(didConnectNet), name: NSNotification.Name(kDidConnetNetNoti) , object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(disConnectNet), name: NSNotification.Name(kDisConnetNetNoti), object: nil)
    }
    
    open func didConnectNet() {
        //网络连接上
    }
    
    open func disConnectNet() {
        //网络断开
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }
    
    //禁用滑动返回手势
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
    }
    
    override open var shouldAutorotate: Bool {
        return false
    }
    
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    open override func onNetworkResponse<T>(id: Int, resp: NetworkResp, model: T) -> AnyObject? where T : BaseModel<AnyObject> {
        
        let json = JSON(resp.getData())
        print("json \(json)")
        if json == nil {
            return NetworkErr(Int(NetworkStatus.kNulltCode)!, NetworkStatus.kNullTips);
        }
        let model:BaseModel = JSONDeserializer<T>.deserializeFrom(json: resp.getText())!
     
        let code: String = model.code ?? NetworkStatus.kDefaultCode;
        let err: String = model.err ?? NetworkStatus.kErrorTips;
        guard Int(code) == 0 else {
            return NetworkErr(Int(code)!, err);
        }
       
        return model.data as AnyObject;
    }
    
    open override func onNetworkErr<T>(id: Int, err: T) where T : NetworkErr {
        
    }
}


open class BaseTableViewCon<T, HOLDER: IBCellHolder>: TableViewConEx<T, HOLDER> {
    
    override open var shouldAutorotate: Bool {
        return false
    }
    
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    open override func onNetworkResponse<T>(id: Int, resp: NetworkResp, model: T) -> AnyObject? where T : BaseModel<AnyObject> {
        
        let json = JSON(resp.getData())
        print("json \(json)")
        if json == nil {
            return nil;
        }
        let model:BaseModel = JSONDeserializer<T>.deserializeFrom(json: resp.getText())!
        
        let code: String = model.code ?? NetworkStatus.kDefaultCode;
        let err: String = model.err ?? NetworkStatus.kErrorTips;
        guard Int(code) == 0 else {
            return NetworkErr(Int(code)!, err);
        }
        
        return model.data as AnyObject;
    }
}

open class BaseGroupTableViewCon<T, HOLDER: IBCellHolder>: GroupTableViewConEx<T, HOLDER>  {
    
}
